package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.MessageMapper;
import edu.gxnu.cetcdemo.pojo.Message;
import edu.gxnu.cetcdemo.service.MessageService;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【message】的数据库操作Service实现
* @createDate 2024-04-02 15:09:11
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Result queryMessage() {
        Map data = new HashMap();
        List<Message> messageList = messageMapper.selectList(new LambdaQueryWrapper<Message>().eq(Message::getMessageReplyId, 0));
        data.put("messageList", messageList);
        return Result.build(200, "成功查询所有留言", data);
    }

    @Override
    public Result queryReply(long id) {
        Map data = new HashMap();
        List<Message> replyList = messageMapper.selectList(new LambdaQueryWrapper<Message>().eq(Message::getMessageReplyId, id));
        data.put("replyList", replyList);
        return Result.build(200, "成功查询该留言下的所有回复", data);
    }

    @Override
    public Result addMessage(Message message) {
        // 设置留言本身的回复的ID为自己
        message.setMessageReplyId(0);
        messageMapper.insert(message);
        return Result.build(200, "留言发布成功");
    }

    @Override
    public Result addReply(Message message, long id) {
        // 设置留言回复的ID
        message.setMessageReplyId((int) id);
        messageMapper.insert(message);
        return Result.build(200, "回复发布成功");
    }

    @Override
    public Result delMessage(long id) {
        messageMapper.delete(new LambdaQueryWrapper<Message>().eq(Message::getMessageId, id));
        return Result.build(200, "留言删除成功");
    }
}




